package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 使用反射机制访问注解
 */
public class ReflectDemo7 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //查看Student类那些方法含有@AutoRun注解
        Class cls = Class.forName("reflect.Student");
        Object obj = cls.newInstance();//实例化一个Student
        //获取Student所有本类的方法
        Method[] methods = cls.getDeclaredMethods();
        for(Method method : methods){
            //判断该方法是否被@AutoRun修饰过
            if(method.isAnnotationPresent(AutoRun.class)){
                System.out.println(method.getName()+":被AutoRun修饰!");
                method.invoke(obj);
            }else{
                System.out.println(method.getName()+":没有被AutoRun修饰!");
            }
        }
    }
}
